package message.types.unique;

import java.io.IOException;

import message.Message;
import message.visitor.MessageVisitor;
import network.e2e.FrameReader;
import network.e2e.FrameWriter;
import stalker.Stalker;
import stalker.StalkerParser;

public class RoomInvitation extends Message {

  private final Stalker location_;
  private final Stalker inviter_;
  
   /**
   * Creates a new invitation for the corresponding id
   */
  public RoomInvitation(Stalker client, Stalker destination, Stalker location, Stalker inviter) {
    super(client, destination);
    assert location != null && inviter != null;
    location_ = location;
    inviter_ = inviter;
  }
  
  /**
   * Retrieves this message over the net
   * @throws IOException 
   */
  public RoomInvitation(Stalker client, Stalker destination, FrameReader reader) throws IOException {
    super(client, destination);
    assert reader != null;
    location_ = StalkerParser.retrieve(reader);
    inviter_ = StalkerParser.retrieve(reader);
    assert location_ != null && inviter_ != null;
  }
  
  public Stalker getLocation() {
    return location_;
  }
  
  public Stalker getInviter() {
    return inviter_;
  }
  
  @Override
  public <T> T accept(MessageVisitor<T> visitor) {
    return visitor.visit(this);
  }

  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof RoomInvitation)) {
      return false;
    }
    RoomInvitation that = (RoomInvitation) obj;
    return this.getClient().equals(that.getClient()) &&
           this.getDestination().equals(that.getDestination()) &&
           this.getLocation().equals(that.getLocation()) &&
           this.getInviter().equals(that.getInviter());
  }

  @Override
  public void send(FrameWriter writer) throws IOException {
    StalkerParser.send(this.getLocation(), writer);
    StalkerParser.send(this.getInviter(), writer);
  }

  @Override
  public String toString() {
    return "RoomInvitation. where: " + this.getLocation() +
    " inviter: " + this.getInviter() + " from: " + this.getClient() + " to: " + this.getDestination();
  }

}
